{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eabcb5a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c636c82e",
   "metadata": {},
   "source": [
    "# pandas分组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b2a6b4f",
   "metadata": {},
   "source": [
    "- groupby()\n",
    "- .groups属性，查看各行的分组情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3d04c50e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>green</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>blue</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yellow</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>green</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>blue</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>yellow</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>yellow</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    color  price\n",
       "0   green      4\n",
       "1    blue      5\n",
       "2  yellow      6\n",
       "3   green      3\n",
       "4    blue      2\n",
       "5  yellow      1\n",
       "6  yellow      7"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pandas.DataFrame(\n",
    "    data = {\n",
    "        'color':['green','blue','yellow','green','blue','yellow','yellow'],\n",
    "        'price':[4,5,6,3,2,1,7]\n",
    "    }\n",
    ")\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "83fd6925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x00000216C63CC390>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照color进行分组\n",
    "df.groupby(by='color')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "37ac324a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'blue': [1, 4], 'green': [0, 3], 'yellow': [2, 5, 6]}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用groups查看分组情况\n",
    "df.groupby(by='color').groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd1e4569",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分组 + 聚合\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
